﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace socketClient
{
    class Program
    {
        static void Main(string[] args)
        {
           // Test();
            int port = 6000;
            string host = "127.0.0.1";//服务器端ip地址

            IPAddress ip = IPAddress.Parse(host);
            IPEndPoint ipe = new IPEndPoint(ip, port);

            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(ipe);

            while (true)//长链接  不关闭clientsocket就可以了
            {
                Thread.Sleep(3000);
                string sendStr = "send to server : hello,ni hao";
                byte[] sendBytes = Encoding.ASCII.GetBytes(sendStr);
                clientSocket.Send(sendBytes);

                //receive message
                string recStr = "";
                byte[] recBytes = new byte[4096];
                int bytes = clientSocket.Receive(recBytes, recBytes.Length, 0);
                recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
                Console.WriteLine(recStr);
            }
            //send message

            clientSocket.Close();
            Console.ReadKey();
        }

        public static void Test()
        {
            Task.Delay(5 * 1000).ContinueWith((x) =>
            {
                Console.WriteLine("ssss");
                //dosomething
                Test();
            });
        }

    }
}
